<?php

/**
 * 投票
 * 单选多选投票
 * @category Vote
 * @package  Plus
 * @author   ayhome <ayhome@gmail.com>
 * @version  $Id: VoteAction.php 2013-12-19 21:20:03 ayhome@gmail.com $
 */
class VoteAction extends BasePlusAction {
    ////////////////微信插件的基础信息//////////////////

    /////唯一编码标识 以此为检查更新 
    public function code()
    {
      return "vote";
    }

    ///插件的描述
    public function desc()
    {
      return "单选多选投票";
    }

    ///插件版本号
    public function ver()
    {
      return "20131220";
    }

    ///创建时间 默认
    public function ctime()
    {
      return '2013-12-20';
    }

    ///插件名称
    public function title()
    {
      return "投票";
    }
    ////////////////微信插件的基础信息//////////////////

    /**
    * 会将微信的所有参数传递过来,全局变量为 $this->wxpost; 
    * 这里做了处理之后 返回给前端处理再返回给用户;
    **/
    public function getMsg($value='')
    {
      $keyword = $this->wxpost['Content'];

      if ($keyword == "") return;
      $map['keyword'] = $keyword ;
      $map['mid'] = $this->wxpost['mid'];
        $PlusVote = M('PlusVote');
      $v  = $PlusVote->where($map)->find();
      if (!$v) return;

      $volist = array();
      $vo['title'] = $v['title'];
      $plusid = $this->myPlusFull['id'];
      $vo['url'] = U("Mobile/Plus/index?id=$v[id]&plusid=$plusid",'','',false,true);
      $vo['desc'] = $v['intro'];
      $volist[] = $vo;
      $this->retMsg['msgType'] = 'news';
      $this->retMsg['volist'] = $volist;
      return $this->retMsg;
    }
    
    /////  安装插件时候的动作.例如 创建表
    public function install()
    {
        $Model = new Model();
        $Model->execute("CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."plus_vote` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `mid` int(11) NOT NULL,  `title` varchar(128) NOT NULL, `keyword` varchar(128) NOT NULL,  `desc` text NOT NULL COMMENT '补充说明',  `issingle` smallint(6) NOT NULL COMMENT '是否单选',  `intro` varchar(255) NOT NULL COMMENT '投票后显示的文字',  `ctime` int(11) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ");
        $Model->execute( $sql);
        $Model->execute("CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."plus_vote_item` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `mid` int(11) NOT NULL,  `vid` int(11) NOT NULL,  `title` varchar(128) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1");
        $Model->execute( $sql);
        $Model->execute("CREATE TABLE IF NOT EXISTS `yun_plus_vote_user` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `vid` int(11) NOT NULL,  `itemid` int(11) NOT NULL,  `ctime` int(11) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1");
        $Model->execute( $sql);
    }

    /////  插件卸载时候的动作.例如 删除表
    public function uninstall()
    {
      // if ($this->mid) {
      //   $Model = new Model();
      //   $sql = "DROP TABLE IF EXISTS `".C('DB_PREFIX')."plus_vote`";
      //   $Model->execute($sql);
      //   $sql = "DROP TABLE IF EXISTS `".C('DB_PREFIX')."plus_vote_item`";
      //   $Model->execute($sql);
      //   $sql = "DROP TABLE IF EXISTS `".C('DB_PREFIX')."plus_vote_user`";
      //   $Model->execute($sql);
      // }
    }


    ///列表
    public function index()
    {
          $PlusVote = M('PlusVote');
        $map['mid'] = $this->mid;
        $volist  = $PlusVote->where($map)->select();
        $this->assign('volist',$volist);
        cookie( '_currentUrl_', __SELF__ );
    }

    ///插入新增动作
    public function insert()
    {
        $_POST['mid'] = $this->mid;
        $_POST['ctime'] = time();
        $PlusVote = D("PlusVote");
        if ( false === $PlusVote->create() ) {
            $this->error( $PlusVote->getError() );
        }
        //保存当前数据对象
        $list = $PlusVote->add();
        if ( $list !== false ) { //保存成功
            $this->success( '新增成功!', cookie( '_currentUrl_' ) );
        } else {
          $this->error( '新增失败!' );
        }
    }
    //编辑邀请函
    public function edit()
    {
        $PlusVoteItem = M('PlusVoteItem');
        $id = I('id');
        $map['id'] = $id;
        $map['mid'] = $this->mid;
        $PlusVote = D("PlusVote");
        $vo = $PlusVote->where( $map )->find();
        if ( !$vo ) {
            $this->error( "参数错误.非法参数." );
            exit();
        }
        unset($map);
        $map['vid'] = $id;
        $itemlist = $PlusVoteItem->where($map)->select();
        $this->assign( 'itemlist', $itemlist );
        $this->assign( 'vo', $vo );
    }

    ///ajax  插入选项
    public function insertitem()
    {
        $PlusVoteItem = M('PlusVoteItem');
        $vid = I('vid');
        $title = I('title');
        $data['title'] = $title;
        $data['vid'] = $vid;
        $data['mid'] = $this->mid;
        $PlusVoteItem->add($data);
    }

    // ajax 更新
    public function updatevote()
    {
      $PlusVote = M('PlusVote');
      $id = I('id');
      if (!$id) return;
      $fvalue = I('fvalue');
      $fname = I('fname');
      $data[$fname] = $fvalue;
      $map['id'] = $id;
      $map['mid'] = $this->mid;
      $PlusVote->where($map)->save($data);
      exit();
    }

    public function updateitem($value='')
    {
      $PlusVoteItem = M('PlusVoteItem');
      $id = I('id');
      if (!$id) return;
      $title = I('title');
      $map['id'] = $id;
      $data['title'] = $title;
      $PlusVoteItem->where($map)->save($data);
      exit();
    }


    ///删除数据
    public function delete()
    {
        $PlusVoteItem = M('PlusVoteItem');
        $PlusVote = D("PlusVote");
        $id = I('id');
        $map['id'] = $id;
        $map['mid'] = $this->mid;
        $PlusVote->where($map)->delete();
        unset($map);
        $map['vid'] = $id;
        $PlusVoteItem->where($map)->delete();
        $this->success( '删除成功!');
        exit();
    }

    public function deleteitem()
    {
        $id = I('itemid');
        if ($id == 0) return;
        $map['mid'] = $this->mid;
        $map['id'] = $id;
        $PlusVoteItem = M('PlusVoteItem');
        $PlusVoteItem->where($map)->delete();
        exit();
    }


    /////
    //移动端显示
    public function indexMobile()
    {
        
      $PlusVote = D("PlusVote");
        $id = I('id');
        $vo = $PlusVote->where()->find($id);

      $plusid = I('plusid');
      $PlusVoteUser = D("PlusVoteUser");
      $map['vid'] = $id;

      $PlusVoteItem = M('PlusVoteItem');
      $itemlist = $PlusVoteItem->where($map)->select();
      $this->assign( 'itemlist', $itemlist );

      //是否投票
      $wxid_key = "void_".$id;
      if (cookie($wxid_key) || I('ac') == 'tongji') {
        $itemuser =  array();
        unset($map);
        $map['vid'] = $id;
        $counts = $PlusVoteUser->where($map)->count();
        $itemuser['counts'] = $counts;
        foreach ($itemlist as $k) {
          unset($map);
          $map['itemid'] = $k['id'];
          $count = $PlusVoteUser->where($map)->count();
          $k['count'] = $count;
          $k['respeed'] = 1 - round(($count / $counts),2);
          $k['speed'] = round(($count / $counts),2);
          $itemuser['list'][] = $k;
        }
          
        $this->assign( 'itemlist', $itemuser );
        // print_r($itemuser);
        // exit();
      }
      unset($map);
      $map['vid'] = $id;
      $usercount = $PlusVoteUser->where( $map )->count();
      if (!$usercount) $usercount = 0;
      $this->assign( 'usercount', $usercount );
      $this->assign( 'plusid', $plusid );
      $this->assign( 'vo', $vo );
    }


    ///投票
    public function join()
    {
        $vid = I('vid');
        $itemid = I('itemid');
        $wxid_key = "void_".$vid;
        if (cookie($wxid_key)) {
          $data['state']  = 1;
          $this->ajaxReturn($data);
        }
        
        $PlusVoteUser = M('PlusVoteUser');
        $data['vid'] = $vid;
        $data['itemid'] = $itemid;
        $data['ctime'] = time();
        $uuid =$PlusVoteUser->add($data);
        setcookie($wxid_key, "true", time()+9999999,"/");
        $data['state']  = $uuid;
        $data['html']  = $uuid;
        $this->ajaxReturn($data);
        exit();
    }

    function retHtml($itemid = 0){
      //   if ($itemid == 0) return;
      // //<div class='attch_item'><div><div class='option_title'>小指</div><div class='option_info'>956票&nbsp;4.9%</div><div class='clear'></div></div><div><div class='option_green' style='width:4.9%;'></div><div class='option_gray' style='width:95.1%;'></div><div class='clear'></div></div><div class='option_vote'></div></div>
      //   $PlusVoteUser = M('PlusVoteUser');
      //   $map['itemid'] = $itemid;
    }



}